﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;

namespace Rose.Helper
{
    public class ShareHelpModel
    {
        private Database_RoseEntities db = new Database_RoseEntities();


        public bool AddShareToDatabase(string selfId,string link,string description)
        {
            try
            {
                Share addModel = new Share()
                {
                    Id = Guid.NewGuid(),
                    CreateTime = DateTime.Now,
                    UserId=selfId,
                    TargetLink=CheckDataHelpModel.CheckLength(link,400),
                    Description=CheckDataHelpModel.CheckLength(description,140)
                };
                db.AddToShares(addModel);

                UserInfo userinfo = db.UserInfos.Where(c => c.Id == selfId).FirstOrDefault();
                if (userinfo == null)
                    throw new ArgumentNullException();
                userinfo.Score += int.Parse(Rose.ViewRes.Defaultresx.ShareAddScore);

                new NewTrendHelpModel().AddShareTrend(addModel);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        public bool RemoveShareFromDatabase(string id, string selfId)
        {
            try
            {
                Guid guidId = new Guid(id);
                Share deleteModel = db.Shares.Where(c => c.Id == guidId && c.UserId == selfId).FirstOrDefault();
                if (deleteModel == null)
                    return true;
                db.Shares.DeleteObject(deleteModel);
                db.SaveChanges();
                return true;
            }
            catch
            { return false; }

        }

        public List<Share> GetShareList(string selfId, int currentpage, int pagesize)
        {
            
            return db.Shares.Where(c => c.UserId == selfId).OrderByDescending(c=>c.CreateTime).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();

        }
        public int GetShareCount(string selfId)
        {
            return db.Shares.Where(c => c.UserId == selfId).Count();
        }

    }
}